<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 11/25/14
 * Time: 4:39 PM
 */

namespace Admin;

use AdminController,
    Config,
    Jinput,
    Input,
    Validator,
    View,
    Redirect,
    Singer,
    MyModel,
    Sentry,
    Lang,
    Str;

class SingersController extends AdminController
{
    public function getIndex()
    {
        $data['paginate']['appends']['name'] = Jinput::get('name', '');
        $data['paginate']['nums'] = 1;
        $myModel = new MyModel();
        $arrParams = array();
        if ($data['paginate']['appends']['name'] != '') {
            $arrParams['conditions'][] = array('field' => 'name', 'opr' => 'LIKE', 'value' => '%' . $data['paginate']['appends']['name'] . '%');
        } else {
            unset($data['paginate']['appends']['name']);
        }
        $arrParams['paginate'] = $data['paginate'];
        $data['singers'] = $myModel->listItems('Singer', $arrParams);
        return View::make('backend/singers/index', compact('data'));
    }

    public function getCreate()
    {
        $data['singer'] = new Singer();
        return View::make('backend/singers/create', compact('data'));
    }

    public function getEdit($id)
    {
        $data['singer'] = Singer::find($id);
        return View::make('backend/singers/create', compact('data'));
    }

    public function getDelete($id)
    {

    }

    public function getRestore($id)
    {

    }

    public function postCreate()
    {
        $data = Input::get('data');
        $rules = array('name' => 'required');
        $validation = Validator::make($data['Singer'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        $myModel = new MyModel();
        $id = $myModel->saveItem('Singer', $data['Singer']);
        return Redirect::route('update/singers/admin', $id)->with('success', 'Thông tin ca sĩ đã được lưu');
    }
} 